﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;

namespace FinWeb3.Models.Repository
{
    public class BudgetRepository : IRepository<Budget>
    {
        public void Save(Budget item, IIdentity user)
        {
            if (item.Id == 0)
            {
                item.UserName = user.Name;
                item.Save();
            }
            else
            {
                var budget = Get(item.Id, user);

                if (budget != null)
                {
                    budget.Category = item.Category;
                    budget.FromDate = item.FromDate;
                    budget.ToDate = item.ToDate;
                    budget.ShowAt = item.ShowAt;
                    budget.UserName = user.Name;

                    budget.Save();
                }
            }
        }

        public void Delete(Budget item, IIdentity user)
        {
            var budget = Get(item.Id, user);

            if (budget != null)
                budget.Delete();
        }

        public Budget Get(int id, IIdentity user)
        {
            return Budget.All().Where(x => x.UserName == user.Name && x.Id == id).FirstOrDefault();
        }

        public IQueryable<Budget> GetAll(IIdentity user)
        {
            return Budget.All().Where(x => x.UserName == user.Name);
        }
    }
}
